package com.qf.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsOrderMapper;
import com.qf.pojo.DtsOrder;
import com.qf.vo.CategorySellAmts;
import com.qf.vo.DayStatis;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author mkr
 * @version V1.0
 * @Project java2203-mkr
 * @Package com.qf.service
 * @Description:
 * @Date 2022/6/17 9:34
 */
@Service
public class OrderServiceImpl implements  OrderService {

    @Autowired
    private DtsOrderMapper orderMapper;

    @Override
    public int countOrder() {
        QueryWrapper<DtsOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", false);
        Integer count = orderMapper.selectCount(queryWrapper);
        return count;
    }

    @Override
    public List<DayStatis> incrCount() {
        List<DayStatis> list = orderMapper.staticIncresOrderCnt();
        return list;
    }

    @Override
    public List<CategorySellAmts> categoryCount() {
        List<CategorySellAmts> list = orderMapper.categorySell();
        return list;
    }

    @Override
    public IPage<DtsOrder> orderList(Integer userId, String orderSn, Integer orderStatus, Integer page,
                                     Integer limit, String sort, String order) {
        //1.创建分页对象
        IPage<DtsOrder> iPage = new Page<>(page , limit);

        //2.封装查询对象
        QueryWrapper<DtsOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", false);

        //3.判断查询条件是否为空
        if (userId != null){
            queryWrapper.eq("user_id", userId);
        }
        if (!StringUtils.isEmpty(orderSn)){
            queryWrapper.eq("order_sn", orderSn);
        }
        if (orderStatus != null){
            queryWrapper.eq("order_status", orderStatus);
        }

        //4.判断分页条件是否为空
        if( !StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order) ){
            //判断排序方式
            if (order.equalsIgnoreCase("asc")){
                queryWrapper.orderByAsc(sort);
            }else {
                queryWrapper.orderByDesc(sort);
            }
        }
        //5.查询并返回
        IPage<DtsOrder> orderIPage = orderMapper.selectPage(iPage, queryWrapper);
        return orderIPage;
    }
}
